//
//  ChatTableViewCell.m
//  BGM
//
//  Created by 我要变好看 on 2018/12/12.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "ChatTableViewCell.h"
#import "Common.h"
#import "BGMUser.h"
#import "UIImageView+WebCache.h"
@interface ChatTableViewCell ()

@property (nonatomic,strong)UIImageView *headImage;

@property (nonatomic,strong)UIImageView *bgTxtImage;
@property (nonatomic,strong)UILabel *txtLabel;
@property (nonatomic,strong)UIButton *aboveBtn;


@property (nonatomic,strong)NSString *userId;
@property (nonatomic,strong)NIMMessage *message;
@property (nonatomic,strong)UIImageView *voiceImage;


@end


@implementation ChatTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self != nil) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        self.headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
        self.headImage.layer.cornerRadius = 20;
        self.headImage.layer.masksToBounds = YES;
        self.headImage.userInteractionEnabled = YES;
        [self addSubview:self.headImage];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickHead)];
        [self.headImage addGestureRecognizer:tap];
        
        
        self.bgTxtImage = [[UIImageView alloc]init];
        [self addSubview:self.bgTxtImage];
        
        self.voiceImage =[[UIImageView alloc]init];
        [self.bgTxtImage addSubview:self.voiceImage];
        self.voiceImage.hidden = YES;
        
        
        self.txtLabel = [[UILabel alloc]init];
        self.txtLabel.numberOfLines = 0;
        self.txtLabel.textColor = bgmgrayColor;
        self.txtLabel.font = [UIFont systemFontOfSize:15];
        [self.bgTxtImage addSubview:self.txtLabel];
        
        
        self.aboveBtn = [[UIButton alloc]init];
        [self addSubview:self.aboveBtn];
        [self.aboveBtn addTarget:self action:@selector(clickChatMsg) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
    
    
}

- (void)chatContent:(NIMMessage *)message{
    
    self.voiceImage.hidden = YES;

    self.message = message;
    NSString *meUerId = [BGMUser shareInstance].userId;
    NSString *formId = message.from;
    NIMUser *user = [[[NIMSDK sharedSDK]userManager]userInfo:formId];
    self.userId = user.userId;
    NIMUserInfo *yunInfo = user.userInfo;
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:yunInfo.avatarUrl] placeholderImage:[UIImage imageNamed:@"headImage"]];

    NSString *txt;
    if (message.messageType == NIMMessageTypeNotification) {
        txt = @"感谢大家来帮我的忙~";
        self.txtLabel.text = txt;
    }else if (message.messageType == NIMMessageTypeAudio){
        NIMAudioObject *audio = message.messageObject;
        txt = [NSString stringWithFormat:@"%d''",(int)audio.duration];
        self.txtLabel.text = txt;
    }
    else{
        txt = [NSString stringWithFormat:@"%@",message.text];
        if([txt isEqualToString:@"(null)"]){
            self.txtLabel.text = @"";
        }else{
            self.txtLabel.text = txt;

        }
    }
    
    UIFont * tfont = [UIFont systemFontOfSize:15];
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
    CGSize  actualsize =[txt boundingRectWithSize:CGSizeMake(SCREENW-130, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    float imageW = actualsize.width+40;
    float imageH = actualsize.height+25;

    if (![meUerId isEqualToString:formId]) { //左
        self.headImage.frame = CGRectMake(10, 10, 40, 40);
        UIImage *image = [UIImage imageNamed:@"meText"];
        NSInteger leftCapWidth = image.size.width*0.4;
        NSInteger topCapHeight = image.size.height*0.2;
        UIImage *newImage = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];;
        [self.bgTxtImage setImage:newImage];
        
        if (message.messageType == NIMMessageTypeAudio) {
            self.voiceImage.hidden = NO;
            [self.voiceImage setImage:[UIImage imageNamed:@"chatVioce1"]];
            
            NIMAudioObject *audio = message.messageObject;
            float maxLength = SCREENW-130;
            float msgLength = (maxLength/60)*audio.duration;
            if (msgLength<70) {
                msgLength = 70;
            }else if (msgLength>SCREENW-80){
                msgLength = SCREENW-100;
            }
            
            self.bgTxtImage.frame = CGRectMake(55, 20, msgLength, imageH);
            self.voiceImage.frame =CGRectMake(15, 10, 15, 20) ;
            self.txtLabel.frame = CGRectMake(35, 0, self.bgTxtImage.frame.size.width-40, imageH);
            
        }else{
            
            self.bgTxtImage.frame = CGRectMake(55, 20, imageW, imageH);
            self.txtLabel.frame = CGRectMake(20, 0, imageW-40, imageH);
            
        }
        
    }else{
        
        self.headImage.frame = CGRectMake(SCREENW-70, 10, 40, 40);
//        [self.headImage sd_setImageWithURL:[NSURL URLWithString:userImage] placeholderImage:[UIImage imageNamed:@"headImage"]];
        UIImage *image = [UIImage imageNamed:@"othText"];
        NSInteger leftCapWidth = image.size.width*0.7;
        NSInteger topCapHeight = image.size.height*0.5;
        
        UIImage *newImage = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];;
        [self.bgTxtImage setImage:newImage];

        
        if (message.messageType == NIMMessageTypeAudio) {
            self.voiceImage.hidden = NO;
            [self.voiceImage setImage:[UIImage imageNamed:@"chatVioce2"]];
            
            NIMAudioObject *audio = message.messageObject;
            float maxLength = SCREENW-130;
            float msgLength = (maxLength/60)*audio.duration;
            if (msgLength<70) {
                msgLength = 70;
            }
            
            self.bgTxtImage.frame = CGRectMake(SCREENW-msgLength-60-20, 20, msgLength, imageH);
            self.voiceImage.frame =CGRectMake(self.bgTxtImage.frame.size.width-30, 10, 15, 20) ;
            self.txtLabel.frame = CGRectMake(0, 0, self.bgTxtImage.frame.size.width-40, imageH);
            self.txtLabel.textAlignment = NSTextAlignmentRight;
 
        }else{
            
            self.bgTxtImage.frame = CGRectMake(SCREENW-actualsize.width-60-55, 20, imageW, imageH);
            self.txtLabel.frame = CGRectMake(20, 0, imageW-40, imageH);

        }
        
        
    }
    
    self.aboveBtn.frame = self.bgTxtImage.frame;

 
}







+ (CGFloat)chatContentH:(NIMMessage *)message{
    NSString *txt = [NSString stringWithFormat:@"%@",message.text];
    UIFont * tfont = [UIFont systemFontOfSize:15];
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
    CGSize  actualsize =[txt boundingRectWithSize:CGSizeMake(SCREENW-130, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    float imageH = actualsize.height+25;
    return imageH>60 ? imageH+20 : 60;
}
- (void)clickHead{
    [self.chatCellDelegate findOtherInfo:self.userId];
}
- (void)clickChatMsg{
    [self.chatCellDelegate clickMsg:self.message];
}
@end
